#!/usr/bin/env python
#-*- coding: utf-8 -*-

import gtk, gobject

class ObjectList:

    def __init__(self, tree):
    
        self.tree = tree
        
        self.routesLS = gtk.ListStore(gobject.TYPE_STRING, object)
        
        cell = gtk.CellRendererText()
        column = gtk.TreeViewColumn('Obiekt', cell, text=0)
        self.tree.append_column(column)
        
    def switchModel(self, objType=None):
        
        self.tree.get_selection().unselect_all()        
        
        if objType == None:
            model = None        
        elif objType == 'route':
            model = self.routesLS
        
        self.tree.set_model(model)
    
    def addObject(self, emitter, obj, data=None):
        if data == 'route':
            self.routesLS.append((obj.ID, obj))
            
    def delObject(self, emitter, obj, data=None):  
        if data == 'route':
            model, paths = self.tree.get_selection().get_selected_rows()
            if model == self.routesLS and \
                len(paths) == 1 and \
                self.routesLS[paths[0]][1] == obj:
                iter = self.routesLS.get_iter(paths[0])
                valid = self.routesLS.remove(iter) 
                if valid:
                    self.tree.get_selection().select_iter(iter)  
            else:
                for path, row in enumerate(self.routesLS):
                    if row[1] == obj:
                        del self.routesLS[path]
                        break
    
    def delAll(self):
        self.routesLS.clear()
        